package org.fizz.viper;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * HTTP连接的类
 * @author FIZZ
 * @version viper-1.0, 2014-4-17
 * @since 1.0
 */
public class ViperHTTPConnection {

	/**
	 * 默认连接超时时间为3000毫秒
	 * @since 1.0
	 */
	private static final int TIMEOUT_CONN = 3000;
	/**
	 * 默认读超时时间为10000毫秒
	 * @since 1.0
	 */
	private static final int TIMEOUT_READ = 10000;
	
	/**
	 * 匹配字符集的常量
	 * @since 1.0
	 */
	private static final String CHARSET_STRING = "charset=";
	
	/**
	 * 默认读取页面内容时，使用的字符集
	 * @since 1.0
	 */
	private static final String DEFAULT_CHARSET = "GBK";
	
	/**
	 * URL
	 * @since 1.0
	 */
	private URL url;
	
	/**
	 * HTTP连接
	 * @since 1.0
	 */
	private HttpURLConnection connection;
	
	/**
	 * 获得一个HTTP连接的对象
	 * @param urlStr URL的字符串
	 * @since 1.0
	 */
	public ViperHTTPConnection(String urlStr){
		this(urlStr, null);
	}
	
	/**
	 * 获得一个HTTP连接的对象
	 * @param urlStr URL的字符串
	 * @param proxy 代理，如果没有代理,传入<tt>null<tt>即可
	 * @since 1.0
	 */
	public ViperHTTPConnection(String urlStr,ViperHTTPProxy proxy){
		try {
			url = new URL(urlStr);
			if(proxy != null)
				connection = (HttpURLConnection)url.openConnection(proxy);
			else
				connection = (HttpURLConnection)url.openConnection();
			
			initConfig();//初始化配置
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 获得一个HTTP连接的对象
	 * @param urlStr URL的字符串
	 * @param proxyIPAddress 代理服务器的IP地址
	 * @param proxyPort 代理服务器的端口
	 * @param proxyUsername 认证代理服务器的用户名
	 * @param proxyPassword 认证代理服务器的密码
	 * @since 1.0
	 */
	public ViperHTTPConnection(String urlStr,String proxyIPAddress, int proxyPort,
			String proxyUsername, String proxyPassword){
		this(urlStr, ViperHTTPProxy.getInstance(proxyIPAddress, proxyPort, proxyUsername, proxyPassword));
	}
	
	/**
	 * 获得一个HTTP连接的对象
	 * @param urlStr URL的字符串
	 * @param proxyIPAddress 代理服务器的IP地址
	 * @param proxyPort 代理服务器的端口
	 * @since 1.0
	 */
	public ViperHTTPConnection(String urlStr,String proxyIPAddress, int proxyPort){
		this(urlStr, ViperHTTPProxy.getInstance(proxyIPAddress, proxyPort));
	}
	
	/**
	 * 返回HTML流
	 * @return HTML流
	 * @since 1.0
	 */
	public InputStream getHTMLStream(){
		try {
			return connection.getInputStream();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 初始化配置
	 * @since 1.0
	 */
	private void initConfig(){
		connection.setConnectTimeout(TIMEOUT_CONN);//设置连接超时时间
		connection.setReadTimeout(TIMEOUT_READ);//设置连接后从 Input 流读入时的超时时间
		try {
			connection.connect();  
			 //将分别返回 200 和 401。如果无法从响应中识别任何代码（即响应不是有效的 HTTP），则返回 -1。 
			int respCode = connection.getResponseCode();
			if(respCode != -1){
				System.out.println(url.getPath() + " Response Code: " + respCode );
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 返回页面的字符集
	 * @return 页面的字符集
	 * @since 1.0
	 */
	public String getEncoding(){
		String contentType = connection.getContentType();
		String encoding = null;
		if(contentType.indexOf(CHARSET_STRING) != -1){
			 encoding = contentType.substring(contentType.indexOf(CHARSET_STRING) + CHARSET_STRING.length());
		}
		if(encoding == null){
			encoding = DEFAULT_CHARSET;
		}
		return encoding;
	}
}
